package school30.appserver;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * This servlet generates and outputs jar with os and selected apps.
 *
 * @author Samarin Arthur
 */
public class Download extends HttpServlet {
    public final static String OS_PATH = "/os/School30API.jar";
    public final static String APPS_PATH = "/apps/";

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/java-archive;");
        ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
        try {
            InputStream osis = getClass().getResourceAsStream(OS_PATH);
            extractAndAdd(new ZipInputStream(osis), "", zos);
            Enumeration<String> params = request.getParameterNames();
            while(params.hasMoreElements())
            {
                String param = params.nextElement();
                if (param.startsWith("app"))
                {
                  // Parameter is application checkbox
                  InputStream ais = getClass().getResourceAsStream(APPS_PATH + param + ".jar");
                  extractAndAdd(new ZipInputStream(ais), "", zos);
                  //extractAndAdd(, "", zos);
                }
            }
        } finally {
            zos.close();
        }
    }

    /**
     * Extracts files from first zip and adds to another
     *
     * @param zis - Archive where files will be extracted from
     * @param prefix - Directory in new archive
     * @param zos - new archive
     */
    protected void extractAndAdd(ZipInputStream zis, String prefix, ZipOutputStream zos) throws IOException {
        // File which is copying now
        ZipEntry zipentry;
        // Reading buffer
        byte[] buf = new byte[1024];
        // byte
        int n;
        zipentry = zis.getNextEntry();
        while (zipentry != null) {
            //for each entry to be extracted
            String entryName = zipentry.getName();

            ZipEntry ze = new ZipEntry(prefix + entryName);
            zos.putNextEntry(ze);

            while ((n = zis.read(buf, 0, 1024)) > -1) {
                zos.write(buf, 0, n);
            }

            zis.closeEntry();
            zos.closeEntry();
            
            zipentry = zis.getNextEntry();
        }

        zis.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
